#include "head.h"

int reset_state(sqlite3 *db, const char *new_state, const char *new_Ip, int new_Port, int new_sock_fd)
{
    const char *sql = "UPDATE users SET  state =?, IP = ? , port = ?, Sock_fd =? ";
    sqlite3_stmt *stmt;

    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, new_state, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 2, new_Ip, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 3, new_Port);
    sqlite3_bind_int(stmt, 4, new_sock_fd);

    // 执行更新
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        fprintf(stderr, "更新失败: %s\n", sqlite3_errmsg(db));
    }

    sqlite3_finalize(stmt);                    // 释放语句对象
    return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
}
int update_state_by_id(sqlite3 *db, int id, const char *new_state)
{
    const char *sql = "UPDATE users SET state = ? WHERE id = ?";
    sqlite3_stmt *stmt;

    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, new_state, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 2, id);

    // 执行更新
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        fprintf(stderr, "更新失败: %s\n", sqlite3_errmsg(db));
    }

    sqlite3_finalize(stmt);                    // 释放语句对象
    return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
}

int update_password_by_id(sqlite3 *db, int id, const char *new_password)
{
    const char *sql = "UPDATE users SET password = ? WHERE id = ?";
    sqlite3_stmt *stmt;

    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, new_password, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 2, id);

    // 执行更新
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        fprintf(stderr, "更新失败: %s\n", sqlite3_errmsg(db));
    }

    sqlite3_finalize(stmt);                    // 释放语句对象
    return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
}

int update_name_by_id(sqlite3 *db, int id, const char *new_name)
{
    const char *sql = "UPDATE users SET username = ? WHERE id = ?";
    sqlite3_stmt *stmt;

    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, new_name, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 2, id);

    // 执行更新
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        fprintf(stderr, "更新失败: %s\n", sqlite3_errmsg(db));
    }

    sqlite3_finalize(stmt);                    // 释放语句对象
    return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
}





int update_client_data_by_id(sqlite3 *db, int id, const char *new_Ip, int new_Port, int new_Sockfd)
{
    const char *sql = "UPDATE users SET IP = ? , port = ?, Sock_fd =? WHERE id = ?";
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, new_Ip, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 2, new_Port);
    sqlite3_bind_int(stmt, 3, new_Sockfd);
    sqlite3_bind_int(stmt, 4, id);

    // 执行更新
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        fprintf(stderr, "更新失败: %s\n", sqlite3_errmsg(db));
    }

    sqlite3_finalize(stmt);                    // 释放语句对象
    return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
}

int send_client_by_id(sqlite3 *db, int id, char *msg, int send_Sock_fd)
{
    int Sock_fd;
    const char *sql = "SELECT Sock_fd FROM users WHERE id = ?";
    sqlite3_stmt *stmt;

    // 准备 SQL 语句
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定 ID 参数
    sqlite3_bind_int(stmt, 1, id);
    // 执行查询并获取结果
    rc = sqlite3_step(stmt);
    if (rc == SQLITE_ROW)
    {
        // 获取 Sock_fd
        Sock_fd = sqlite3_column_int(stmt, 0);
        printf("id : %d ,Sock_fd: %d", id, Sock_fd);
    }
    else if (rc == SQLITE_DONE)
    {
        printf("未找到 ID 为 %d 的记录。\n", id);
    }
    else
    {
        fprintf(stderr, "查询失败: %s\n", sqlite3_errmsg(db));
    }
    sqlite3_finalize(stmt); // 释放语句对象
    printf("\nsend  id :%d   Sock_fd :  %d -> msg : %s\n", id, Sock_fd, msg);
    if (send_Sock_fd != Sock_fd)
    {
        send(Sock_fd, msg, strlen(msg), 0);
    }

    // send(Sock_fd, msg, strlen(msg), 0);

    return rc == SQLITE_ROW ? SQLITE_OK : rc; // 返回操作结果
}






void findFriendIDsByNameID(sqlite3 *db, int name_ID, int Sock_fd)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT Friends_ID FROM Friends WHERE name_ID = ?;";

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return;
    }

    // 绑定 name_ID 参数
    sqlite3_bind_int(stmt, 1, name_ID);
    char send_buff[BUFFER_SIZE_MAX];
    memset(send_buff, 0, sizeof(send_buff));
    // 执行查询并输出结果

    char title[128] = {"\nFriendsname\tstate\trequest\n"};
    strcat(send_buff, title);

    while (sqlite3_step(stmt) == SQLITE_ROW)
    {
        char send_buf[256];
     //   char name_buff[128];
        int friendID = sqlite3_column_int(stmt, 0);

        printf("Friend ID: %d\n", friendID);

        sprintf(send_buf, "%s\t\t %s \t%s \n", sql_get_username_by_id(db, friendID), sql_get_state_by_id(db, friendID),sql_get_request_by_id(db, name_ID ,friendID));

        strcat(send_buff, send_buf);
    }
    printf("get_frd : %s ", send_buff);
    send(Sock_fd, send_buff, strlen(send_buff), 0);
    // 清理
    sqlite3_finalize(stmt);
}

void findgroup_name_ByNameID(sqlite3 *db, int name_ID, int Sock_fd)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT group_name , admin  FROM group_data WHERE name_ID = ?;";

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return;
    }

    // 绑定 name_ID 参数
    sqlite3_bind_int(stmt, 1, name_ID);
    char send_buff[BUFFER_SIZE_MAX];
    memset(send_buff, 0, sizeof(send_buff));
    // 执行查询并输出结果

    char title[128] = {"\ngroup name\tadmin\n"};
    strcat(send_buff, title);

    while (sqlite3_step(stmt) == SQLITE_ROW)
    {
        char send_buf[256];
     //   char name_buff[128];
        const unsigned char * group_name = sqlite3_column_text(stmt, 0);
        int  user_grade = sqlite3_column_int(stmt, 1);
        printf("group name: %s\n", group_name);
        if (1==user_grade)
        {
             sprintf(send_buf, "%s\t\t%s\n", group_name,"管理员" );
        }else if (0==user_grade)
        {
             sprintf(send_buf, "%s\t\t%s\n", group_name,"普通成员" );
        }
    
        strcat(send_buff, send_buf);
    }
    printf("get_frd : %s ", send_buff);
    send(Sock_fd, send_buff, strlen(send_buff), 0);
    // 清理
    sqlite3_finalize(stmt);
}





int find_Friend(sqlite3 *db, int name_ID, int ID)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT Friends_ID FROM Friends WHERE name_ID = ?;";

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return -1;
    }

    // 绑定 name_ID 参数
    sqlite3_bind_int(stmt, 1, name_ID);
    char send_buff[BUFFER_SIZE_MAX];
    memset(send_buff, 0, sizeof(send_buff));
    // 执行查询并输出结果

    char title[128] = {"Friendsname\tstate\n"};
    strcat(send_buff, title);

    while (sqlite3_step(stmt) == SQLITE_ROW)
    {
        int friendID = sqlite3_column_int(stmt, 0);
        if (ID == friendID)
        {
            sqlite3_finalize(stmt);
            return 0;
        }
        if (!strcmp("off", sql_get_state_by_id(db, friendID)))
        {
            return -2;
        }
    }
    // 清理
    sqlite3_finalize(stmt);
    return -1;
}

int sql_get_group_by_groupname(sqlite3 *db, const char *groupname, int Sock_fd, char *buff)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT name_ID FROM Group_data WHERE group_name = ?;";

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return -1;
    }

    // 绑定 name_ID 参数
    sqlite3_bind_text(stmt, 1, groupname, -1, SQLITE_STATIC);
    char send_buff[BUFFER_SIZE_MAX];

    memset(send_buff, 0, sizeof(send_buff));

    char title[128] = {"Friendsname\tstate\n"};
    strcat(send_buff, title);

    while (sqlite3_step(stmt) == SQLITE_ROW)
    {
        int name_ID = sqlite3_column_int(stmt, 0);
        printf("name_ID : %d\n", name_ID);
        char send_buff[2048];
        sprintf(send_buff, "[msg][group][%s]: %s", groupname, buff);

        send_client_by_id(db, name_ID, send_buff, Sock_fd);
    }
    // 清理
    sqlite3_finalize(stmt);
    return -1;
}




int find_group(sqlite3 *db, const char *group_name)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT COUNT(*) FROM group_data WHERE Group_name = ?;";
    int group_exists = 0;

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return -1; // 返回 -1 表示出错
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, group_name, -1, SQLITE_STATIC);

    // 执行查询并检查结果
    if (sqlite3_step(stmt) == SQLITE_ROW)
    {
        group_exists = sqlite3_column_int(stmt, 0); // 获取计数
    }

    // 清理
    sqlite3_finalize(stmt);

    return group_exists > 0 ? 1 : 0; // 返回 1 表示存在，返回 0 表示不存在
}




int find_ID_by_group(sqlite3 *db, const char *group_name,int ID)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT COUNT(*) FROM group_data WHERE Group_name = ? AND name_ID= ?;";
    int group_exists = 0;

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return -1; // 返回 -1 表示出错
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, group_name, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 2, ID);
    // 执行查询并检查结果
    if (sqlite3_step(stmt) == SQLITE_ROW)
    {
        group_exists = sqlite3_column_int(stmt, 0); // 获取计数
    }

    // 清理
    sqlite3_finalize(stmt);

    return group_exists > 0 ? 1 : 0; // 返回 1 表示存在，返回 0 表示不存在
}



int send_username_by_groupname(sqlite3 *db, const char *group_name,int Sock_fd)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT name  ,   admin FROM group_data WHERE Group_name = ?;";
    int group_exists = 0;

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return -1; // 返回 -1 表示出错
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, group_name, -1, SQLITE_STATIC);
    // 执行查询并检查结果
    char send_buff[BUFFER_SIZE_MAX];
    memset(send_buff, 0, sizeof(send_buff));

    char title[128] = {"\n群成员\t成员权限\n"};
    strcat(send_buff, title);
    int conut=0;
    while  (sqlite3_step(stmt) == SQLITE_ROW)
    {   char buff[BUFFER_SIZE];
        memset(buff, 0, sizeof(buff));
        const unsigned char * user_name = sqlite3_column_text(stmt, 0); // 获取计数
        int grader  =   sqlite3_column_int(stmt,1);
        if (1==grader)
        {
           sprintf(buff ,"%s\t%s\n",user_name,"管理员");
        }else
        {
            sprintf(buff ,"%s\t%s\n",user_name,"普通成员");
        }
        strcat(send_buff, buff);
        conut++;
    }
    printf("群总人数\t%d\n %s",conut,send_buff);

     send(Sock_fd, send_buff, strlen(send_buff), 0);

    // 清理
    sqlite3_finalize(stmt);

    return group_exists > 0 ? 1 : 0; // 返回 1 表示存在，返回 0 表示不存在
}






/*
未完成函数  - >>    先完成   功能     使用  name_ID 获取 数据库中msg
*/
void send_msg_by_Friends(sqlite3 *db, int name_ID, int Sock_fd)
{
    sqlite3_stmt *stmt;
    const char *sql = "SELECT Friends_ID FROM Friends WHERE name_ID = ?;";

    // 准备 SQL 语句
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
        return;
    }

    // 绑定 name_ID 参数
    sqlite3_bind_int(stmt, 1, name_ID);
    char send_buff[BUFFER_SIZE_MAX];
    memset(send_buff, 0, sizeof(send_buff));
    // 执行查询并输出结果

    char title[128] = {"\n-----------朋友圈 ---------\n"};
    strcat(send_buff, title);
    char my_buff[BUFFER_SIZE];
    memset(my_buff, 0, sizeof(my_buff));
    sprintf(my_buff, "%s\n", sql_print_msgs_by_id(db, name_ID, sql_get_username_by_id(db,name_ID)));
    strcat(send_buff, my_buff);
    while (sqlite3_step(stmt) == SQLITE_ROW)
    {
        char send_buf[256];
        char name_buff[128];
        int friendID = sqlite3_column_int(stmt, 0);
        strcpy(name_buff, sql_get_username_by_id(db, friendID));
        sprintf(send_buf, "%s\n", sql_print_msgs_by_id(db, friendID, name_buff));
        strcat(send_buff, send_buf);
    }
    
    printf("get_frd : %s ", send_buff);

    send(Sock_fd, send_buff, strlen(send_buff), 0);
    // 清理
    sqlite3_finalize(stmt);
}

int post_Friends(sqlite3 *db, char *name, int name_ID, char *Friends, int Friends_ID)
{
   int rc = insert_Friends(db, Friends, Friends_ID, name, name_ID, "wait"); 
   int rc2= insert_Friends(db, name, name_ID, Friends, Friends_ID, "wait");
    return  (rc > rc2 )? rc :rc2 ;
}







int agree_Friends_fnc(sqlite3 *db, char *name, int name_ID, char *Friends, int Friends_ID)
{   //UPDATE users SET password = ? WHERE id = ?";
    const char *sql = "UPDATE Friends SET  state = ? WHERE  name = ? AND name_ID = ? AND Friends_name =? AND Friends_ID = ?;";
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法准备 SQL: %s\n", sqlite3_errmsg(db));
        return rc;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, "agree", -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 3, name_ID);
    sqlite3_bind_text(stmt, 4, Friends, -1, SQLITE_STATIC);
    sqlite3_bind_int(stmt, 5, Friends_ID);

    // 执行更新
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        fprintf(stderr, "更新失败: %s\n", sqlite3_errmsg(db));

        sqlite3_finalize(stmt); // 释放语句对象
        return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
    }

    sqlite3_finalize(stmt); // 释放语句对象

  
    return rc == SQLITE_DONE ? SQLITE_OK : rc; // 返回操作结果
}


int agree_Friends(sqlite3 *db, char *name, int name_ID, char *Friends, int Friends_ID)
{

    int rc = agree_Friends_fnc(db,name,name_ID,Friends,Friends_ID);
    int rc2= agree_Friends_fnc(db,Friends,Friends_ID,name,name_ID);

    return  (rc > rc2 )? rc :rc2;
}

